package com.ruoyi.controller;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.pojo.Activities;
import com.ruoyi.pojo.Coupon;
import com.ruoyi.pojo.User;
import com.ruoyi.service.ActivitiesService;
import com.ruoyi.service.CouponService;
import com.ruoyi.service.LogService;
import com.ruoyi.service.UserService;
import com.ruoyi.until.R;
import io.lettuce.core.RedisException;
import org.redisson.Redisson;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisOperations;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import java.util.HashMap;
import java.util.List;
import java.util.Objects;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

/**
 * @author: liufei
 * @Desc:
 * @create: 2024-12-07 09:46
 **/
@RestController
@RequestMapping("/seckill")
public class seckillController {

    @Autowired
    private ActivitiesService activitiesService;

    @Autowired
    private CouponService couponService;

    @Autowired
    private UserService userService;

    @Autowired
    private LogService logService;

    @Autowired
    private RedisTemplate redisTemplate;

    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Autowired
    private RedissonClient redissonClient;

    @Autowired
    private RabbitTemplate rabbitTemplate;



    @GetMapping("/activitiesList")
    public R list(){
        RedisOperations operations = redisTemplate.opsForList().getOperations();
        List<Activities> activitiesList = operations.opsForList().range("activitiesList", 0, -1);
        if (activitiesList.size()==0){
            List<Activities> activitiesList1 = activitiesService.list();
            redisTemplate.opsForList().leftPush("activitiesList", activitiesList1);
            System.out.println(activitiesList1);
            return R.ok(activitiesList1);
        }else{
            return R.ok(activitiesList);
        }
    }

    @GetMapping("/couponList")
    public R couponList(){
        RedisOperations operations = redisTemplate.opsForList().getOperations();
        List<Coupon> couponList = operations.opsForList().range("couponList", 0, -1);
        if (couponList.size()==0){
            List<Coupon> couponList1 = couponService.list();
            for (Coupon coupon : couponList1) {
                //优惠卷剩余数量
                stringRedisTemplate.opsForValue().set("coupon:CouponName:"+coupon.getCouponName(), coupon.getSurplusQuantity());
                //优惠卷可领取数量
                stringRedisTemplate.opsForValue().set("coupon:claimableQuantity:"+coupon.getCouponName(), coupon.getClaimableQuantity());
                //活动是否过期
                stringRedisTemplate.opsForValue().set("coupon:CouponValidity:"+coupon.getCouponName(), coupon.getCouponValidity());
            }

            redisTemplate.opsForList().leftPush("couponList", couponList1);
            return R.ok(couponList1);
        }else{
            return R.ok(couponList);
        }
    }

    @GetMapping("/getRabbmq")
    public void get(){

        //获取用户剩余优惠券数量
        Object obj = stringRedisTemplate.opsForHash().get("user", "刘飞");
        //获取这个活动每个人可以抢什么
        String number1 = stringRedisTemplate.opsForValue().get("coupon:claimableQuantity:" + "双十一满100返50");
        //获取剩余优惠券数量
        String number2 = stringRedisTemplate.opsForValue().get("coupon:CouponName:" + "双十一满100返50");
        //活动是否过期
        String time = stringRedisTemplate.opsForValue().get("coupon:CouponValidity:" + "双十一满100返50");

        System.out.println(obj);
        System.out.println(number1);
        System.out.println(number2);
        System.out.println(time);
    }

    @GetMapping("/user/{id}")
    public R user(@PathVariable Integer id) {

        QueryWrapper<User> wrapper = new QueryWrapper<>();
        wrapper.eq("id", id);
        User user = userService.getOne(wrapper);
        stringRedisTemplate.opsForHash().put("user", user.getUsername(), "0");
        return R.ok(user);
    }

    public void SeckillService() {
        // 配置 Redis 连接
        Config config = new Config();
        config.useSingleServer().setAddress("redis://127.0.0.1:6379");
        this.redissonClient = Redisson.create(config);
    }

    @PostMapping("/havCoupon/{couponName}/{userName}")
    public void SendRabbitMq(@PathVariable String couponName,@PathVariable String userName) {

        HashMap<String, Object> hashMap = new HashMap<>();
        hashMap.put("couponName", couponName);
        hashMap.put("userName", userName);

        String json = JSON.toJSONString(hashMap);

        String msg = UUID.randomUUID().toString();

        stringRedisTemplate.opsForValue().set(msg, json);

        rabbitTemplate.convertAndSend("CouponExchange","coupon.#",msg);
    }












}
